﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Adrr.DAL
{
	/// <summary>
	/// 数据访问类:VersionNo
	/// </summary>
	public partial class VersionNo
	{
		public VersionNo()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("VersionID", "VersionNo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int VersionID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from VersionNo");
			strSql.Append(" where VersionID=@VersionID");
			SqlParameter[] parameters = {
					new SqlParameter("@VersionID", SqlDbType.Int,4)
			};
			parameters[0].Value = VersionID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Adrr.Model.VersionNo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into VersionNo(");
			strSql.Append("PhoneType,PublishTime,VerName,VerDesp,IsMustUp,CreateTime,LastVersion,Operation,Status,DownloadUrl)");
			strSql.Append(" values (");
			strSql.Append("@PhoneType,@PublishTime,@VerName,@VerDesp,@IsMustUp,@CreateTime,@LastVersion,@Operation,@Status,@DownloadUrl)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@PhoneType", SqlDbType.VarChar,20),
					new SqlParameter("@PublishTime", SqlDbType.DateTime),
					new SqlParameter("@VerName", SqlDbType.NVarChar,50),
					new SqlParameter("@VerDesp", SqlDbType.NVarChar,500),
					new SqlParameter("@IsMustUp", SqlDbType.Bit,1),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@LastVersion", SqlDbType.Int,4),
					new SqlParameter("@Operation", SqlDbType.VarChar,5),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@DownloadUrl", SqlDbType.VarChar,256)};
			parameters[0].Value = model.PhoneType;
			parameters[1].Value = model.PublishTime;
			parameters[2].Value = model.VerName;
			parameters[3].Value = model.VerDesp;
			parameters[4].Value = model.IsMustUp;
			parameters[5].Value = model.CreateTime;
			parameters[6].Value = model.LastVersion;
			parameters[7].Value = model.Operation;
			parameters[8].Value = model.Status;
			parameters[9].Value = model.DownloadUrl;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Adrr.Model.VersionNo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update VersionNo set ");
			strSql.Append("PhoneType=@PhoneType,");
			strSql.Append("PublishTime=@PublishTime,");
			strSql.Append("VerName=@VerName,");
			strSql.Append("VerDesp=@VerDesp,");
			strSql.Append("IsMustUp=@IsMustUp,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("LastVersion=@LastVersion,");
			strSql.Append("Operation=@Operation,");
			strSql.Append("Status=@Status,");
			strSql.Append("DownloadUrl=@DownloadUrl");
			strSql.Append(" where VersionID=@VersionID");
			SqlParameter[] parameters = {
					new SqlParameter("@PhoneType", SqlDbType.VarChar,20),
					new SqlParameter("@PublishTime", SqlDbType.DateTime),
					new SqlParameter("@VerName", SqlDbType.NVarChar,50),
					new SqlParameter("@VerDesp", SqlDbType.NVarChar,500),
					new SqlParameter("@IsMustUp", SqlDbType.Bit,1),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@LastVersion", SqlDbType.Int,4),
					new SqlParameter("@Operation", SqlDbType.VarChar,5),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@DownloadUrl", SqlDbType.VarChar,256),
					new SqlParameter("@VersionID", SqlDbType.Int,4)};
			parameters[0].Value = model.PhoneType;
			parameters[1].Value = model.PublishTime;
			parameters[2].Value = model.VerName;
			parameters[3].Value = model.VerDesp;
			parameters[4].Value = model.IsMustUp;
			parameters[5].Value = model.CreateTime;
			parameters[6].Value = model.LastVersion;
			parameters[7].Value = model.Operation;
			parameters[8].Value = model.Status;
			parameters[9].Value = model.DownloadUrl;
			parameters[10].Value = model.VersionID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int VersionID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from VersionNo ");
			strSql.Append(" where VersionID=@VersionID");
			SqlParameter[] parameters = {
					new SqlParameter("@VersionID", SqlDbType.Int,4)
			};
			parameters[0].Value = VersionID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string VersionIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from VersionNo ");
			strSql.Append(" where VersionID in ("+VersionIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Adrr.Model.VersionNo GetModel(int VersionID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 VersionID,PhoneType,PublishTime,VerName,VerDesp,IsMustUp,CreateTime,LastVersion,Operation,Status,DownloadUrl from VersionNo ");
			strSql.Append(" where VersionID=@VersionID");
			SqlParameter[] parameters = {
					new SqlParameter("@VersionID", SqlDbType.Int,4)
			};
			parameters[0].Value = VersionID;

			Adrr.Model.VersionNo model=new Adrr.Model.VersionNo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["VersionID"]!=null && ds.Tables[0].Rows[0]["VersionID"].ToString()!="")
				{
					model.VersionID=int.Parse(ds.Tables[0].Rows[0]["VersionID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["PhoneType"]!=null && ds.Tables[0].Rows[0]["PhoneType"].ToString()!="")
				{
					model.PhoneType=ds.Tables[0].Rows[0]["PhoneType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PublishTime"]!=null && ds.Tables[0].Rows[0]["PublishTime"].ToString()!="")
				{
					model.PublishTime=DateTime.Parse(ds.Tables[0].Rows[0]["PublishTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["VerName"]!=null && ds.Tables[0].Rows[0]["VerName"].ToString()!="")
				{
					model.VerName=ds.Tables[0].Rows[0]["VerName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["VerDesp"]!=null && ds.Tables[0].Rows[0]["VerDesp"].ToString()!="")
				{
					model.VerDesp=ds.Tables[0].Rows[0]["VerDesp"].ToString();
				}
				if(ds.Tables[0].Rows[0]["IsMustUp"]!=null && ds.Tables[0].Rows[0]["IsMustUp"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsMustUp"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsMustUp"].ToString().ToLower()=="true"))
					{
						model.IsMustUp=true;
					}
					else
					{
						model.IsMustUp=false;
					}
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LastVersion"]!=null && ds.Tables[0].Rows[0]["LastVersion"].ToString()!="")
				{
					model.LastVersion=int.Parse(ds.Tables[0].Rows[0]["LastVersion"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Operation"]!=null && ds.Tables[0].Rows[0]["Operation"].ToString()!="")
				{
					model.Operation=ds.Tables[0].Rows[0]["Operation"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Status"]!=null && ds.Tables[0].Rows[0]["Status"].ToString()!="")
				{
					model.Status=int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
				}
				if(ds.Tables[0].Rows[0]["DownloadUrl"]!=null && ds.Tables[0].Rows[0]["DownloadUrl"].ToString()!="")
				{
					model.DownloadUrl=ds.Tables[0].Rows[0]["DownloadUrl"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select VersionID,PhoneType,PublishTime,VerName,VerDesp,IsMustUp,CreateTime,LastVersion,Operation,Status,DownloadUrl ");
			strSql.Append(" FROM VersionNo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" VersionID,PhoneType,PublishTime,VerName,VerDesp,IsMustUp,CreateTime,LastVersion,Operation,Status,DownloadUrl ");
			strSql.Append(" FROM VersionNo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM VersionNo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.VersionID desc");
			}
			strSql.Append(")AS Row, T.*  from VersionNo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "VersionNo";
			parameters[1].Value = "VersionID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

